package br.com.mtech.ctrlliberty.mobile;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public class AndroidWebService {

	public static final String NAMESPACE = "http://service.ejb.ctrlliberty.mtech.com.br/";
	public static final String SOAP_ACTION = "SynchronizerWebServiceEJB";
	public static final String URL = "http://10.0.2.2:8080/ctrlLibertyEJB/SynchronizerWebServiceEJB";
	
	public static void sincronizarDados(String ddd, Long numeroCel, String tipoDado, String json){
		
		Log.i("CTRLLIBERTY", "Inicio webservice");
		
		SoapObject request = new SoapObject(NAMESPACE, "sincronizarDados");
		
		PropertyInfo p1 = new PropertyInfo();
		PropertyInfo p2 = new PropertyInfo();
		PropertyInfo p3 = new PropertyInfo();
		PropertyInfo p4 = new PropertyInfo();
		
		p1.setName("ddd"); 
		p1.setValue(ddd);
		p1.setType(String.class);
		
		p2.setName("telefone"); 
		p2.setValue(numeroCel); 
		p2.setType(Long.class); 
		
		p3.setName("tipoEntidade"); 
		p3.setValue(tipoDado); 
		p3.setType(String.class); 
		
		p4.setName("jsonListaEntidade"); 
		p4.setValue(json); 
		p4.setType(String.class);
		
		request.addProperty(p1); 
		request.addProperty(p2); 
		request.addProperty(p3); 
		request.addProperty(p4);
		
		Log.i("CTRLLIBERTY", "meio webservice");
				
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		envelope.setOutputSoapObject(request);
		
		HttpTransportSE transport = new HttpTransportSE(URL);
		
		try {
			// Efetua a chamada ao WS passando o envelope instanciado
			transport.call(SOAP_ACTION, envelope);
			// Encapsula a resposta, no caso, para um objeto do tipo Cliente
			SoapObject response = (SoapObject) envelope.getResponse();
			
		} catch(Exception e) {
			Log.e("CtrlLiberty", "Fudeu !!! : " + envelope);
			
			e.printStackTrace();
		}
	}
	
}
